package com.lzx.demo.bloodpressurehelper.fragment.home.adapter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lzx.demo.bloodpressurehelper.R;
import com.lzx.demo.bloodpressurehelper.bean.CalendarBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/10/28.
 */
public class CalendarAdapter extends BaseAdapter {
    private List<CalendarBean> calendarBeens;
    private Context mContext;
    private SQLiteDatabase db;

    public CalendarAdapter(List<CalendarBean> calendarBeens, Context mContext,SQLiteDatabase db) {
        this.calendarBeens = calendarBeens;
        this.mContext = mContext;
        this.db = db;
    }

    @Override
    public int getCount() {
        return calendarBeens.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_calendar_day, parent, false);
            holder = new ViewHolder();
            holder.calendarDayTsext = (TextView) convertView.findViewById(R.id.calendar_day_text);
            holder.bloodPressure = (TextView) convertView.findViewById(R.id.blood_pressure);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        String dayText = ((calendarBeens.get(position).getDay()) == null) ? " " : calendarBeens.get(position).getDay().toString();
        holder.calendarDayTsext.setText(dayText);
        if(calendarBeens.get(position).getWeek()==0||calendarBeens.get(position).getWeek()==6) {
            holder.calendarDayTsext.setTextColor(Color.parseColor("#F7602C"));
        }
        CalendarBean calendarBean = calendarBeens.get(position);
        initBloodPressure(calendarBean,holder);
        return convertView;
    }

    private void initBloodPressure(CalendarBean calendarBean,ViewHolder holder){
        String SQLStr = "SELECT round(avg(HEIGHT_BLOOD_PRESSURE),0) AS HEIGHT_BLOOD_PRESSURE_DAY,\n" +
                "\t\t\t round(avg(LOW_BLOOD_PRESSURE),0) AS LOW_BLOOD_PRESSURE_DAY,\n" +
                "       round(avg(HEART_RATE),1) AS HEART_RATE_DAY\n" +
                "\t\tFROM blood\n" +
                "\t\tWHERE strftime('%Y-%m-%d', remem_time)=?;";
        List<String> queryArgs = new ArrayList<String>();
        String year = calendarBean.getYear().toString();
        String month ="";
        if(calendarBean.getMonth().intValue()>9){
            month=calendarBean.getMonth().toString();
        }else{
            month="0"+calendarBean.getMonth().toString();
        }
        String day = "";
        if (calendarBean.getDay()!=null) {
            if (calendarBean.getDay().intValue() > 9) {
                day = calendarBean.getDay().toString();
            } else {
                day = "0" + calendarBean.getDay().toString();
            }
        }
        queryArgs.add(year+"-"+month+"-"+day);
        Log.e("heh",year+"-"+month+"-"+day);
        Cursor cursor =  db.rawQuery(SQLStr, queryArgs.toArray(new String[queryArgs.size()]));
        if(cursor.moveToFirst()){
            String heightBloodPressure = cursor.getString(cursor.getColumnIndex("HEIGHT_BLOOD_PRESSURE_DAY"));
            String lowBloodPressure = cursor.getString(cursor.getColumnIndex("LOW_BLOOD_PRESSURE_DAY"));
            holder.bloodPressure.setText((heightBloodPressure==null?"":heightBloodPressure)+
                        "/"
                        +(lowBloodPressure==null?"":lowBloodPressure));
        }
    }

    static class ViewHolder{
        TextView calendarDayTsext;
        TextView bloodPressure;
    }
}
